package org.itboys.mobile.controller.site;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.JoinPicture;
import org.itboys.mobile.service.mongo.site.JoinSitePictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017/9/28
 * 描述：雄安加盟网_图库
 */
@Controller
@RequestMapping("/join/site/picture")
public class JoinSitePictureController extends BaseController{
    @Autowired
    private JoinSitePictureService pictureService;

    /**
     * 查询
     * @param request
     * @param response
     * @param memberRecordId
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="memberRecordId",required=true)Long memberRecordId,
                       @RequestParam(value="type",required=false)Integer type
    ) {
        try {
            List<JoinPicture> result=pictureService.select(request,memberRecordId,type);
            AjaxUtils.renderJson(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 新增
     * @param request
     * @param response
     * @param memberRecordId
     * @param type
     * @param title
     * @param url
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="memberRecordId",required=true)Long memberRecordId,
                     @RequestParam(value="type",required=true)Integer type,
                     @RequestParam(value="title",required=true)String title,
                     @RequestParam(value="url",required=true)String url
    ){
        try {
            pictureService.savePicture(memberRecordId,type,title,url);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/picture/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }

    }

    /**
     *删除
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id

    ) {
        try {
            pictureService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/picture/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     *设置状态
     * @param request
     * @param response
     */
    @RequestMapping("/setStatus")
    public void setStatus(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="pictureId",required=true) Long pictureId,
                          @RequestParam(value="isBack",required=false) Integer isBack,
                          @RequestParam(value="isRoll",required=false) Integer isRoll

    ) {
        try {
            pictureService.setStatus(pictureId,isBack,isRoll);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/picture/setStatus------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 设置图片类型
     * @param request
     * @param response
     * @param pictureId
     * @param type
     */
    @RequestMapping("/setType")
    public void setType(HttpServletRequest request,
                          HttpServletResponse response,
                          @RequestParam(value="pictureId",required=true) Long pictureId,
                          @RequestParam(value="type",required=false) Integer type

    ) {
        try {
            pictureService.setType(pictureId,type);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/picture/setType------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
